package com.alks.function.service.serviceImpl.finance.fincons;


import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.finance.fincons.SettlementDetailsDto;
import com.alks.function.data.dto.finance.fincons.SettlementSummaryDto;
import com.alks.function.data.request.finance.fincons.AnnualPayableRequest;
import com.alks.function.data.request.finance.fincons.SettlementPayableRequest;
import com.alks.function.mapper.finance.fincons.SettlementPayableMapper;
import com.alks.function.service.finance.fincons.SettlementPayableService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class SettlementPayableServiceImpl implements SettlementPayableService {

    @Autowired
    private SettlementPayableMapper settlementPayableMapper;

    /**
     * 结算应付明细查询，数量、金额
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getSettlementDetails(SettlementPayableRequest request) {
        //全部：
        //未结算 5714 3109  只有未结算的数据暂时没有对上
        String companyId = UserIdThread.get().getCompanyId();
        request.setCompanyId(companyId);
        List<SettlementDetailsDto> list = settlementPayableMapper.getSettlementDetails(request);

        //统计数量
        BigDecimal qty = BigDecimal.ZERO;
        BigDecimal amt = BigDecimal.ZERO;
        if(ObjectUtils.isNotEmpty(list)){
            for (SettlementDetailsDto dto : list) {
                BigDecimal currentQty = dto.getQty() != null ? dto.getQty() : BigDecimal.ZERO;
                BigDecimal currentAmt = dto.getAmt() != null ? dto.getAmt() : BigDecimal.ZERO;

                qty = qty.add(currentQty);
                amt = amt.add(currentAmt);
            }
        }

        // 设置结果保留四位小数
//        qty = qty.setScale(4, RoundingMode.HALF_UP);
//        amt = amt.setScale(4, RoundingMode.HALF_UP);

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        map.put("qty",qty);
        map.put("amt",amt);
        return ResponseInfo.ok(map);
    }

    /**
     * 结算应付汇总查询，总额、已对账金额、未对账金额
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getSettlementSummary(SettlementPayableRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<SettlementSummaryDto> list = settlementPayableMapper.getSettlementSummaryByMonth(companyId,request);

        //统计数量
        BigDecimal amt = BigDecimal.ZERO;//总金额
        BigDecimal balanceAmt = BigDecimal.ZERO;//已对账金额
        BigDecimal unBalanceAmt = BigDecimal.ZERO;//未对账金额
        BigDecimal apAmt = BigDecimal.ZERO;//已付金额
        BigDecimal unApAmt = BigDecimal.ZERO;//未付金额
        if(ObjectUtils.isNotEmpty(list)){
            for (SettlementSummaryDto dto : list) {
                BigDecimal currentAmt = dto.getAmt() != null ? dto.getAmt() : BigDecimal.ZERO;
                BigDecimal currentBalanceAmt = dto.getBalanceAmt() != null ? dto.getBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentUnBalanceAmt = dto.getUnBalanceAmt() != null ? dto.getUnBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentApAmt = dto.getApAmt() != null ? dto.getApAmt() : BigDecimal.ZERO;
                BigDecimal currentUnApAmt = dto.getUnApAmt() != null ? dto.getUnApAmt() : BigDecimal.ZERO;

                amt = amt.add(currentAmt);
                balanceAmt = balanceAmt.add(currentBalanceAmt);
                unBalanceAmt = unBalanceAmt.add(currentUnBalanceAmt);
                apAmt = apAmt.add(currentApAmt);
                unApAmt = unApAmt.add(currentUnApAmt);
            }
        }

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        map.put("amt",amt);
        map.put("balanceAmt",balanceAmt);
        map.put("unBalanceAmt",unBalanceAmt);
        map.put("apAmt",apAmt);
        map.put("unApAmt",unApAmt);
        return ResponseInfo.ok(map);
    }

    /**
     * 结算应付汇总/月份查询,计算金额
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getSettlementSummaryByMonth(SettlementPayableRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<SettlementSummaryDto> list = settlementPayableMapper.getSettlementSummaryByMonth(companyId,request);

        //统计数量
        BigDecimal amt = BigDecimal.ZERO;//总金额
        BigDecimal balanceAmt = BigDecimal.ZERO;//已对账金额
        BigDecimal unBalanceAmt = BigDecimal.ZERO;//未对账金额
        BigDecimal apAmt = BigDecimal.ZERO;//已付金额
        BigDecimal unApAmt = BigDecimal.ZERO;//未付金额
        if(ObjectUtils.isNotEmpty(list)){
            for (SettlementSummaryDto dto : list) {
                BigDecimal currentAmt = dto.getAmt() != null ? dto.getAmt() : BigDecimal.ZERO;
                BigDecimal currentBalanceAmt = dto.getBalanceAmt() != null ? dto.getBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentUnBalanceAmt = dto.getUnBalanceAmt() != null ? dto.getUnBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentApAmt = dto.getApAmt() != null ? dto.getApAmt() : BigDecimal.ZERO;
                BigDecimal currentUnApAmt = dto.getUnApAmt() != null ? dto.getUnApAmt() : BigDecimal.ZERO;

                amt = amt.add(currentAmt);
                balanceAmt = balanceAmt.add(currentBalanceAmt);
                unBalanceAmt = unBalanceAmt.add(currentUnBalanceAmt);
                apAmt = apAmt.add(currentApAmt);
                unApAmt = unApAmt.add(currentUnApAmt);
            }
        }

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        map.put("amt",amt);
        map.put("balanceAmt",balanceAmt);
        map.put("unBalanceAmt",unBalanceAmt);
        map.put("apAmt",apAmt);
        map.put("unApAmt",unApAmt);
        return ResponseInfo.ok(map);
    }

    /**
     * 结算应付汇总/年度查询,计算金额
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getSettlementSummaryByAnnual(AnnualPayableRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<SettlementSummaryDto> list = settlementPayableMapper.getSettlementPayableSummaryByAnnual(companyId,request);

        //统计数量
        BigDecimal amt = BigDecimal.ZERO;//总金额
        BigDecimal balanceAmt = BigDecimal.ZERO;//已对账金额
        BigDecimal unBalanceAmt = BigDecimal.ZERO;//未对账金额
        BigDecimal apAmt = BigDecimal.ZERO;//已付金额
        BigDecimal unApAmt = BigDecimal.ZERO;//未付金额
        BigDecimal unAmtDq = BigDecimal.ZERO;//到期未付
        BigDecimal unAmtYq = BigDecimal.ZERO;//逾期未付
        if(ObjectUtils.isNotEmpty(list)){
            for (SettlementSummaryDto dto : list) {
                BigDecimal currentAmt = dto.getAmt() != null ? dto.getAmt() : BigDecimal.ZERO;
                BigDecimal currentBalanceAmt = dto.getBalanceAmt() != null ? dto.getBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentUnBalanceAmt = dto.getUnBalanceAmt() != null ? dto.getUnBalanceAmt() : BigDecimal.ZERO;
                BigDecimal currentApAmt = dto.getApAmt() != null ? dto.getApAmt() : BigDecimal.ZERO;
                BigDecimal currentUnApAmt = dto.getUnApAmt() != null ? dto.getUnApAmt() : BigDecimal.ZERO;
//                BigDecimal currentUnAmtDq = dto.getUnAmtDq() != null ? dto.getUnAmtDq() : BigDecimal.ZERO;
//                BigDecimal currentUnAmtYq = dto.getUnAmtYq() != null ? dto.getUnAmtYq() : BigDecimal.ZERO;

                amt = amt.add(currentAmt);
                balanceAmt = balanceAmt.add(currentBalanceAmt);
                unBalanceAmt = unBalanceAmt.add(currentUnBalanceAmt);
                apAmt = apAmt.add(currentApAmt);
                unApAmt = unApAmt.add(currentUnApAmt);
            }
        }

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        map.put("amt",amt);
        map.put("balanceAmt",balanceAmt);
        map.put("unBalanceAmt",unBalanceAmt);
        map.put("apAmt",apAmt);
        map.put("unApAmt",unApAmt);
        return ResponseInfo.ok(map);
    }
}